package com.smxy.daydayup.controller;

import com.smxy.daydayup.mapper.SignMapper;
import com.smxy.daydayup.pojo.Sign;
import com.smxy.daydayup.pojo.vo.HistorySignVo;
import com.smxy.daydayup.pojo.vo.SignVo;
import com.smxy.daydayup.util.datetime.DateUtil;
import com.smxy.daydayup.util.result.ResultUtil;
import com.smxy.daydayup.util.result.ReturnResult;
import com.smxy.daydayup.util.result.StatusCode;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sign")
@Api(tags = "签到控制器")
public class SignController {

    @Autowired
    SignMapper signMapper;

    @GetMapping("/student")
    public ReturnResult studentGetAllSign(@RequestParam("courseId") Long courseId,
                                          @RequestParam("userId") String userId){
        List<SignVo> list = signMapper.selectSignInfoByCourseAndUser(courseId,userId);
        return ResultUtil.returnSuccess(list,"查询成功");
    }
    @GetMapping("/student/sign")
    public ReturnResult studentSign(@RequestParam("courseId") Long courseId,
                                          @RequestParam("userId") String userId,
                                    @RequestParam("signNumber") Integer signNumber){
        int i = signMapper.updateUserSignByUserAndNumberAndCourse(userId, signNumber, courseId);
        if(i>0) {
            return ResultUtil.returnSuccess("查询成功");
        }else{
            return ResultUtil.returnFail(StatusCode.SERVER_EXCEPTION,"错误");
        }
    }
    @GetMapping("/teacher")
    public ReturnResult teacherGetAllSign(@RequestParam("courseId") Long courseId){
        List<HistorySignVo> list = signMapper.selectHistorySignByCourse(courseId);
        Integer count = signMapper.selectCountByCourse(courseId);
        if(count==null){
            count = 0;
        }
        return ResultUtil.returnSuccess(list,""+count);
    }
    @GetMapping("/teacher/not")
    public ReturnResult teacherGetNotSign(@RequestParam("courseId") Long courseId,
                                          @RequestParam("signNumber") Integer signNumber){
        List<String> list = signMapper.selectUserByCourseAndNumber(courseId, signNumber);
        return ResultUtil.returnSuccess(list,"查询成功");
    }
    @GetMapping("/teacher/all")
    public ReturnResult teacherGetAllStudent(@RequestParam("courseId") Long courseId){
        List<String> list = signMapper.selectAllUserByCourse(courseId);
        Integer count = signMapper.selectCountByCourse(courseId);
        if(count==null){
            count = 0;
        }
        return ResultUtil.returnSuccess(list,""+count);
    }
    @GetMapping("/teacher/sign")
    public ReturnResult teacherSign(@RequestParam("courseId") Long courseId){
        Integer max = signMapper.selectMaxByCourse(courseId);
        if(max==null){
            max = 0;
        }
        max++;
        List<String> list = signMapper.selectUserIdByCourse(courseId);
        String time = DateUtil.getNowDate().toString();
        for (int i = 0; i < list.size(); i++) {
            Sign sign = new Sign();
            sign.setSignFlag(0);
            sign.setSignNumber(max);
            sign.setCourseId(courseId);
            sign.setGmtTime(time);
            sign.setUserId(list.get(i));
            signMapper.insertSelective(sign);
        }
        return ResultUtil.returnSuccess("发布成功");
    }
}
